package com.ant.storage.controller;

import com.alibaba.excel.EasyExcel;
import com.ant.storage.common.ResultVo;
import com.ant.storage.dto.ChannelDto;
import com.ant.storage.excel.DepExcel;
import com.ant.storage.excel.ExcelMergeUtil;
import com.ant.storage.mapper.GoodsPoolMapper;
import com.ant.storage.mapper.PurchaseOrdersMapper;
import com.ant.storage.pojo.PurchaseOrders;
import com.ant.storage.pojo.Subscribe;
import com.ant.storage.service.IPickingService;
import com.ant.storage.service.IPurchaseOrdersService;
import com.ant.storage.service.ISubscribeService;
import com.ant.storage.vo.DepVo;
import com.ant.storage.vo.PurchaseReportVo;
import com.ant.storage.vo.WarehouseCensusListVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author hzd
 * @version 1.0
 * @date 2022/12/2 9:25
 */
@RestController
@RequestMapping("/report")
@Api(tags = "报表接口")
public class ReportController {

    @Resource
    GoodsPoolMapper goodsPoolMapper;

    @Resource
    ISubscribeService subscribeService;

    @Resource
    PurchaseOrdersMapper purchaseOrdersMapper;

    @Resource
    IPurchaseOrdersService purchaseOrdersService;

    @Resource
    IPickingService pickingService;


    @ApiOperation(value = "获取报表数据")
    @GetMapping("/getReport")
    public ResultVo getReport(@RequestParam(value = "year",required = false) Integer year,
                              @RequestParam(value = "month",required = false) Integer month) {
        return ResultVo.success("获取成功", goodsPoolMapper.getReport(year,month));
    }


    @ApiOperation(value = "获取采购员个人统计数据")
    @GetMapping("/getBuyerReport/{nameid}")
    public ResultVo getBuyerReport(@PathVariable(value = "nameid", required = false) Integer nameid) {
        List<Subscribe> list = subscribeService.list(new QueryWrapper<Subscribe>()
                .eq("purchaser_id", nameid));
        //条件过滤未报销
        List<Subscribe> collect = list.stream().filter(s -> s.getReimbursement() == 2).collect(Collectors.toList());

        List<PurchaseOrders> purchaseOrdersList = purchaseOrdersMapper.selectList(new QueryWrapper<PurchaseOrders>()
                .eq("purchaser_id", nameid));
        // 统计采购金额
        BigDecimal total = new BigDecimal(0);
        for (Subscribe subscribe : list) {
            total = total.add(subscribe.getPrice());
        }
        //统计单量
        int count = list.size();
        //list统计完成单量
        int finishCount = subscribeService.count(new QueryWrapper<Subscribe>().
                eq("purchaser_id", nameid).in("status", 0, 1));
        Map<String, Object> map = new HashMap<>();
        map.put("totalAmount", total);
        map.put("orderCount", count);
        map.put("completedQuantity", finishCount);
        map.put("settlementTotal", collect.size());
        return ResultVo.success("获取成功", map);
    }


    @ApiOperation(value = "获取采购员个人统计数据")
    @GetMapping("/getReportPurAmount/{nameid}")
    public ResultVo getReportPurAmount(@PathVariable(value = "nameid", required = false) Integer nameid) {
        purchaseOrdersMapper.getReportPurAmount();
        return ResultVo.success("获取成功",null);
    }




    @ApiOperation(value = "部门统计")
    @GetMapping("/getDepCensus")
    public ResultVo getDepCensus(@RequestParam(value = "depId", required = false) Integer depId,
                                 @RequestParam(value = "times", required = false) String[] times) {
        List<DepVo> list = purchaseOrdersMapper.getDepCensus(depId, times);
        Map<String, Object> map = new HashMap<>();
        map.put("total", list.size());
        map.put("list", list);
        return ResultVo.success("获取成功", map);
    }


    @ApiOperation(value = "渠道统计")
    @GetMapping("/getChannelCensus")
    public ResultVo getChannelCensus(ChannelDto dto) {
        List<DepVo> list = purchaseOrdersMapper.getChannelCensus(dto);
        return ResultVo.success("获取成功", list);
    }

    @ApiOperation(value = "渠道统计")
    @GetMapping("/getChannelCensusDetails")
    public ResultVo getChannelCensusDetails(ChannelDto dto) {
        List<DepVo> list = purchaseOrdersMapper.getChannelCensus(dto);
        return ResultVo.success("获取成功", list);
    }


    @ApiOperation(value = "详细信息列表")
    @GetMapping("/getList")
    public ResultVo getList(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                            @RequestParam(value = "pageSize", required = false) Integer pageSize,
                            @RequestParam(value = "depId", required = false) Integer depId,
                            @RequestParam(value = "channel", required = false) Integer channelId,
                            @RequestParam(value = "purchaserId", required = false) Integer purchaserId,
                            @RequestParam(value = "times", required = false) String[] times) {
        List<PurchaseReportVo> list = purchaseOrdersMapper.getList(pageIndex,pageSize,depId, channelId, purchaserId, times);

        Map<String, Object> map = new HashMap<>();
        map.put("total", purchaseOrdersMapper.getListCount(depId, channelId, purchaserId, times));
        map.put("list", list);
        return ResultVo.success("获取成功", map);
       // return ResultVo.success("获取成功", list);
    }


    @ApiOperation(value = "部门统计报表导出")
    @GetMapping("/getDepCensusReport")
    public ResultVo getDepCensusReport(
                            @RequestParam(value = "depId", required = false) Integer depId,
                            @RequestParam(value = "times", required = false) String[] times) {
        List<DepVo> list = purchaseOrdersMapper.getDepCensusReport(depId, times);
        return ResultVo.success("获取成功", list);
        // return ResultVo.success("获取成功", list);
    }

    @ApiOperation(value = "导出部门信息")
    @GetMapping("/downLoadDepByIdData/{depId}")
    public void downLoadDepByIdData(@RequestParam(value = "depId", required = false) Integer depId,
                                     HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("售卖订单列表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xls");
        List<DepExcel> orderList = purchaseOrdersService.exportdep(depId);
        //需要合并的列
        int[] mergeColumeIndex = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
        // 从第二行后开始合并
        int mergeRowIndex = 2;
        EasyExcel.write(response.getOutputStream(), DepExcel.class)
                .sheet("售卖订单列表")
                .registerWriteHandler(new ExcelMergeUtil(mergeRowIndex, mergeColumeIndex))
                .doWrite(orderList);

    }

    // 仓库统计
    @ApiOperation(value = "仓库数量统计")
    @GetMapping("/getWarehouseCensus")
    public ResultVo getWarehouseCensus() {
        return ResultVo.success("获取成功", purchaseOrdersMapper.getWarehouseCensus());
    }

    @ApiOperation(value = "仓库列表")
    @GetMapping("/getWarehouseCensusList")
    public ResultVo getWarehouseCensusList(@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
                                           @RequestParam(value = "pageSize", required = false) Integer pageSize,
                                           @RequestParam(value ="warehouseId", required = false) Integer warehouseId) {

        List<WarehouseCensusListVo> list = purchaseOrdersMapper.getWarehouseCensusList(pageIndex,
                pageSize, warehouseId);
        Map<String, Object> map = new HashMap<>();
        map.put("total", purchaseOrdersMapper.getWarehouseCensusListCount(pageIndex, pageSize, warehouseId));
        map.put("list", list);
        return ResultVo.success("获取成功", map);
    }

    @ApiOperation(value = "仓库出入明细")
    @GetMapping("/getWarehouseCensusdetails/{id}")
    public ResultVo getWarehouseCensusdetails(@PathVariable(value ="id", required = false) Integer id) {
        return ResultVo.success("获取成功",purchaseOrdersMapper.inventoryLog(id));
    }
}
